﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.IO;

namespace Client
{
    public partial class HealthClient : System.Web.UI.Page
    {
        private string getSearchHTML(string bid, string phone, string rating, string specialty, string address, string bname, string dist) 
        {
            string ret = "";
            if (dist.Equals("") == false) bname += " ( " + (dist+".0").Substring(0,3) + " miles)";
            ret += "<div class='myBox'>";
            ret += "<a href='FullProfile.aspx?id=" + bid + "'>" + "<h2>" + bname + "</h2>" + "</a>";
            ret += "<div class='specialty'>" + specialty + "</div>";
            ret += "<br />";
            ret += "<div class='text'>" +  "Address: " + address + "</div>";
            ret += "<div class='text'>" + "Phone:" + phone + "</div>";
            ret += "<br />";
            ret += "<div class='text'>" + "Rating: " + "<span class='big'>" + rating.Substring(0,3) + "</span>" + "</h2>" + "</div>";     
            ret += "</div>";
            ret += "<hr>";
            return ret;
        }
        public void Search(Object sender, EventArgs E)
        {
            SearchResults.Service2 service = new SearchResults.Service2();
            string xml = service.SearchResults(zip.Text, myInput.Value, avgrating.SelectedValue, distance.Text);
            /*
               <business>
    <BusinessId>iBVaM1pUygPcXoextW3T0g</BusinessId>
    <BusinessName>Campbell Medical, Dr. Ganesh</BusinessName>
    <Address>18805 Cox Ave Saratoga, CA</Address>
    <CatId>familydr</CatId>
    <Specialty>Family Practice</Specialty>

    <Phone>4083783100</Phone>
    <Rating>2.333333</Rating>
  </business>

             */
            XmlReader reader = XmlReader.Create(new StringReader(xml));
            string currentElement = "";
            string phone = "", rating = "", specialty = "", address = "", bname = "";
            string bid = "";
            string dist = "";
            int cnt = 0;
            Results.InnerHtml = "";
            Counter.Attributes.Clear();
            //Counter.Attributes.Add("display", "block");
            string lastS = "";
            while (reader.Read())
            {
                switch (reader.NodeType)
                {
                    case XmlNodeType.Element:
                        currentElement = reader.Name;
                        break;
                     case XmlNodeType.Text:
                        switch (currentElement)
                        {
                            case "Phone":
                                phone = reader.Value;
                                break;
                            case "Rating":
                                rating = reader.Value;
                                break;
                            case "Specialty":
                                if (reader.Value.Equals(lastS) == false)
                                {
                                    if (specialty.Equals("") == false) specialty += ", ";
                                    specialty = specialty + reader.Value;
                                    lastS = reader.Value;
                                }
                                break;
                            case "Address":
                                address = reader.Value;
                                break;
                            case "BusinessName":
                                bname = reader.Value;
                                break;
                            case "BusinessId":
                                bid = reader.Value;
                                break;
                            case "Distance":
                                dist = reader.Value;
                                break;
                        }
                        break;
                    case XmlNodeType.EndElement:
                        if (reader.Name.Equals("business")) 
                        {
                            // a found search
                            if (dist.Equals("-1")) continue;
                            ++cnt;
                            Results.InnerHtml += getSearchHTML(bid, phone, rating, specialty, address, bname, dist);
                        }
                        break;
                }//end switch
            } //endwhile
            ResultsCount.InnerHtml = cnt.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
      
        }
    }
}